package api;

import cn.hutool.json.JSONUtil;
import model.UserInfo;
import util.AppVar;
import util.ResultUtils;
import util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * 得到当前用户的登录信息
 */
@WebServlet("/loginUserInfo")
public class LoginUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String msg = "未知错误";
        UserInfo userInfo = SessionUtils.getLoginUser(req);
        //得到session对象
        if(userInfo != null){
            userInfo.setPassword("");
        }
        else {
            msg = "请先登录!";
        }
//        HttpSession session = req.getSession(false);
//        //从session对象获得当前对象
//        if(session != null && session.getAttribute(AppVar.SESSION_USERINFO_KEY) != null){
//            userInfo = (UserInfo) session.getAttribute(AppVar.SESSION_USERINFO_KEY);
//            userInfo.setPassword("");//去除掉返回信息中的敏感信息
//        }
//        else {
//            msg = "未登录";
//        }

        //将用户反返回
        resp.setContentType(AppVar.RESPONSE_CONTENTTYPE_JSON);
        resp.getWriter().println(JSONUtil.toJsonStr(ResultUtils.toResult(200,userInfo,msg)));
    }
}
